package com.appbee.songs.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.listener.ValueChangeListener;
import com.appbee.songs.client.text.SongsStrings;
import com.appbee.songs.shared.FilterField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class SongSearchView extends Composite implements ISongFilterView
{

	private static SongSearchViewUiBinder uiBinder = GWT
			.create(SongSearchViewUiBinder.class);

	interface SongSearchViewUiBinder extends UiBinder<Widget, SongSearchView>
	{
	}
	
	@UiField
	Button searchButton;
	
	@UiField
	TextBox searchBox;
	String currentValue;
	
	private final PopupPanel popupPanel;
	private final FlowPanel flowPanel;
	
	private final RadioButton btAll = new RadioButton("f", "Allt");
	private final RadioButton btName = new RadioButton("f","Nafn");
	private final RadioButton btArtist = new RadioButton("f","Flytjandi");
	private final RadioButton btAlbum = new RadioButton("f","Plata");
	private final RadioButton btGenre = new RadioButton("f","Flokkur");
	private final RadioButton btStartingPlayer = new RadioButton("f","Hver byrjar");
	
	private ValueChangeListener valueChangeListener;

	@Inject
	public SongSearchView(PageStyle style, SongsStrings songsStrings)
	{
		btAll.addStyleName(style.searchOption());
		btName.addStyleName(style.searchOption());
		btArtist.addStyleName(style.searchOption());
		btAlbum.addStyleName(style.searchOption());
		btGenre.addStyleName(style.searchOption());
		btStartingPlayer.addStyleName(style.searchOption());
		initWidget(uiBinder.createAndBindUi(this));
		searchButton.setStyleName(style.searchButton());
		searchBox.getElement().setAttribute("placeholder",
											songsStrings.search());
		popupPanel = new PopupPanel();
		popupPanel.setAutoHideEnabled(true);
		flowPanel = new FlowPanel();
		popupPanel.setWidget(flowPanel);
		
		btAll.setValue(true);
		flowPanel.add(btAll);
		flowPanel.add(btName);
		flowPanel.add(btArtist);
		flowPanel.add(btAlbum);
		flowPanel.add(btGenre);
		flowPanel.add(btStartingPlayer);
		
		searchButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				popupPanel.showRelativeTo(searchButton);
				if(btAll.getValue()) btAll.setFocus(true);
				else if(btName.getValue()) btName.setFocus(true);
				else if(btArtist.getValue()) btArtist.setFocus(true);
				else if(btAlbum.getValue()) btAlbum.setFocus(true);
				else if(btGenre.getValue()) btGenre.setFocus(true);
				else if(btStartingPlayer.getValue()) btStartingPlayer.setFocus(true);
			}
		});
		
		searchBox.addKeyUpHandler(new KeyUpHandler()
		{
			
			@Override
			public void onKeyUp(KeyUpEvent event)
			{
				String value = getValue();
				if(!value.equals(currentValue) && valueChangeListener!=null)
				{
					currentValue = value;
					valueChangeListener.onValueChange(value);
				}
			}
		});
		
		ClickHandler handler = new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				popupPanel.hide();
				valueChangeListener.onValueChange(getValue());
				searchBox.setFocus(true);
			}
		};
		btAll.addClickHandler(handler);
		btName.addClickHandler(handler);
		btAlbum.addClickHandler(handler);
		btArtist.addClickHandler(handler);
		btGenre.addClickHandler(handler);
		btStartingPlayer.addClickHandler(handler);
		
		
	}
	
	@Override
	public String getValue()
	{
		String value = searchBox.getValue();
		return value;
	}
	
	@Override
	public void setValue(String value)
	{
		searchBox.setValue(value);
	}

	@Override
	public FilterField getField()
	{
		if(btAll.getValue()) return FilterField.All;
		else if(btName.getValue()) return FilterField.Name;
		else if(btAlbum.getValue()) return FilterField.Album;
		else if(btArtist.getValue()) return FilterField.Artist;
		else if(btGenre.getValue()) return FilterField.Genre;
		else if(btStartingPlayer.getValue()) return FilterField.StartingPlayer;
		return FilterField.All;
	}

	@Override
	public void setField(FilterField field)
	{
		btAll.setValue(field==FilterField.All);
		btName.setValue(field==FilterField.Name);
		btAlbum.setValue(field==FilterField.Album);
		btArtist.setValue(field==FilterField.Artist);
		btGenre.setValue(field==FilterField.Genre);
		btStartingPlayer.setValue(field==FilterField.StartingPlayer);
		
	}

	public HandlerRegistration addKeyUpHandler(KeyUpHandler handler)
	{
		return searchBox.addKeyUpHandler(handler);
	}


	public void setValueChangeListener(ValueChangeListener valueChangeListener)
	{
		this.valueChangeListener = valueChangeListener;
	}

}
